package io.github.yotta.export.web.simple;

import com.google.common.collect.Lists;
import io.github.yotta.common.api.model.Paging;
import io.github.yotta.export.core.exception.DataExportException;
import io.github.yotta.export.web.anno.ExportExcelData;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * @author yotta
 * @date 2023/10/30 16:09
 */
@Component
public class UserService {

    @ExportExcelData(name = "test", fileName = "用户数据导出注解实现")
    public Paging<UserExportModel> page(UserExportParam exportParam) throws DataExportException {
        final Integer pageNo = exportParam.getPageNo();
        final Integer pageSize = exportParam.getPageSize();
        if (pageNo == 3) {
            // 模拟只有两页数据
            return new Paging<>(2L * exportParam.getPageSize(), Collections.emptyList(), exportParam.getPageNo(), exportParam.getPageSize());
        }
        final List<UserExportModel> userExportModels = Lists.newArrayList();
        for (int i = 0; i < exportParam.getPageSize(); i++) {
            final UserExportModel userExportModel = new UserExportModel();
            userExportModel.setName("Yotta-" + ((pageNo - 1) * pageSize + i + 1));
            userExportModel.setAge(exportParam.getAge());
            userExportModels.add(userExportModel);
        }
        // 模拟查询数据
        return new Paging<>(2L * exportParam.getPageSize(), userExportModels, exportParam.getPageNo(), exportParam.getPageSize());
    }
}
